/* -------------------------------------- 

     Cherrypy webapp skeleton frontend

     License: LGPL
     Authors: saaj (mail@saaj.me)

   -------------------------------------- */

/**
 * File list controller class
 */
qx.Class.define("webapp.controller.File", {

  extend : webapp.controller.Abstract,

	members : {

    _store          : null,
    _listController : null,
    _serviceName    : "application.service.File",
    
    
    _reload : function()
    {
      this._store.reload();
    },
    
    _download : function()
    {
      var selection = this._listController.getSelection();
      if(selection.getLength())
      {
        var model = new webapp.model.FileEditor(this._serviceName);
        model.download(selection.getItem(0).getName());
      }
    },
    
    _create : function()
    {
      var model = new webapp.model.FileEditor(this._serviceName);
      model.addListener("saved", this._reload, this);
      
      var view  = new webapp.view.FileEditor();
      qx.core.Init.getApplication().getRoot().add(view);
      view.setModel(model);
      view.load({});
      view.open();
    },
    
    _remove : function(rowIndex)
    {
      var selection = this._listController.getSelection();
      if(selection.getLength())
      {
        dialog.Dialog.confirm(this.tr("Really remove it?"), function(succeed)
        {
          if(succeed)
          {
            var model = new webapp.model.FileEditor(this._serviceName);
            model.addListenerOnce("saved", this._reload, this);
            model.remove(selection.getItem(0).getName());
          }
        }, this);  
      }
    },
    
		createWidget : function()
		{
      var view             = new webapp.view.FileList();
      this._listController = view.getListController();
     
      this._store = new webapp.network.RpcStore(this._serviceName);
      this._store.bind("model", this._listController, "model");
      this._store.load("list");
      
      view.getButtons().reload.addListener("execute",   this._reload,   this);
      view.getButtons().create.addListener("execute",   this._create,   this);
      view.getButtons().download.addListener("execute", this._download, this);
      view.getButtons().remove.addListener("execute",   this._remove,   this);


      return view;
		}
    
	}

});